<!--- Document Information -----------------------------------------------------

Title:      HibernateFactory.cfc

Author:     Mark Mandel
Email:      mark@compoundtheory.com

Website:    http://www.compoundtheory.com

Purpose:    Factory for getting to Hibernate objects

Usage:      

Modification Log:

Name			Date			Description
================================================================================
Mark Mandel		27/06/2006		Created

------------------------------------------------------------------------------->

<cfcomponent name="HibernateConfiguration" hint="Factory for getting to Hibernate objects">

<cfscript>
	instance = StructNew();
</cfscript>

<cffunction name="getHibernateFactory" access="private" returntype="any" output="false">
	<cfreturn variables.HibernateFactory />
</cffunction>

<cffunction name="setHibernateFactory" access="private" returntype="void" output="false">
	<cfargument name="HibernateFactory" type="any" required="true">
	<cfset variables.HibernateFactory = arguments.HibernateFactory />
</cffunction>

<cffunction name="getHibernateTools" access="private" returntype="any" output="false">
	<cfreturn variables.HibernateTools />
</cffunction>

<cffunction name="setHibernateTools" access="public" returntype="void" output="false">
	<cfargument name="HibernateTools" type="any" required="true">
	<cfset variables.HibernateTools = arguments.HibernateTools />
</cffunction>

<cffunction name="getConfiguration" access="public" returntype="any" output="false">
	<cfreturn variables.Configuration />
</cffunction>

<cffunction name="setConfiguration" access="private" returntype="void" output="false">
	<cfargument name="Configuration" type="any" required="true">
	<cfset variables.Configuration = arguments.Configuration />
</cffunction>


<cffunction name="dumpvar">
  <cfargument name="var" default="blank">
  <cfargument name="abort" default="true">
  <cftry>
	  <cfdump var="#var#">
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>

<!------------------------------------------- PUBLIC ------------------------------------------->

<cffunction name="init" hint="Constructor: Loads the hibernate.cfg.xml from here, and any *.hbm.xml files that are in this directory" access="public" returntype="HibernateConfiguration" output="false">
	<cfargument name="hibernateFactory" hint="hibernate factory" required="Yes">
	<cfscript>
		setHibernateFactory(arguments.hibernateFactory);
		setConfiguration(getHibernateFactory().newConfiguration());		
		return this;
	</cfscript>
</cffunction>

<cffunction name="createHsqldbSchema">
	<cfscript>	
		var myjar = getHibernateFactory().createHsqldbSchema();
	</cfscript>
</cffunction>


<cffunction name="configureByStruct" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfargument name="configparams" required="true" type="struct">
	<cfscript>
		var hibConfig = "";
		var params = arguments.configparams;
		var paramskeys = structKeyList(params);
		try {
			hibConfig = getConfiguration();
			for (p = 1; p lte listLen(paramskeys); p = p+1) {
				hibConfig.setProperty(listGetAt(paramskeys,p), params[listGetAt(paramskeys,p)]);
			}
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		return this;
	</cfscript>
</cffunction>

<cffunction name="configure" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfargument name="hibernateConfigFile" required="true">
	<cfscript>
		var cfgFile = getHibernateFactory().newFile(arguments.hibernateConfigFile);
		var hibConfig = "";
//		var kalxml = getHibernateFactory().newFile(expandPath("/actionpacks/cfhibernate/tests/config/hbm/cfhibernate/tests/Tblkalendar.hbm.xml"));
//		var myjar = getHibernateFactory().newFile(variables.classfilesdir & "../cfhibernate.jar");

		try {

			hibConfig = getConfiguration();
//			hibConfig.addJar(myjar);
//			hibConfig.addDirectory(hbmDir);
//			hibConfig.addFile(kalxml);
			hibConfig.configure(cfgFile);
//			tbl = create("Tblkalendar");
//			hibConfig.addClass(tbl.getClass());
//			session = hibConfig.buildSessionFactory();
 			//sessionFactory = hibConfig.buildSessionFactory();
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		return this;
	</cfscript>
</cffunction>

<cffunction name="addHbmLocation" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfargument name="hbmlocatoin" required="true">
	<cfscript>
		var hbmsLoc = getHibernateFactory().newFile(arguments.hbmlocatoin);
		var hibConfig = getConfiguration();
		try {
 			if(hbmsLoc.isDirectory()) {
				hibConfig.addDirectory(hbmsLoc);
 			} else {
				hibConfig.addJar(hbmsLoc);
 			}
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		return this;
	</cfscript>
</cffunction>

<cffunction name="buildSessionFactory" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfscript>
		var hibConfig = getConfiguration();
		var sessionFactory = "";
		try {
			sessionFactory = hibConfig.buildSessionFactory();
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		return sessionFactory;
	</cfscript>
</cffunction>

<cffunction name="getHibernateVersion" hint="The hibernate version in use" access="public" returntype="string" output="false">
	<cfreturn getHibernateFactory().getHibernateVersion() />
</cffunction>

<!------------------------------------------- PACKAGE ------------------------------------------->

<!------------------------------------------- PRIVATE ------------------------------------------->

<cffunction name="dumpPackages" hint="Inititalises all the java libs to be loaded in" access="public" returntype="void" output="false">
	<cfscript>
		var package = createObject("java","java.lang.Package");
		var packages = package.getPackages();
		var allpacks = arrayNew(1);
		var pack = "";
		var packinfo = "";
		for (x = 1; x lte arrayLen(packages); x = x+1) {
			pack = packages[x];
			packinfo = structNew();
			packinfo["name"] = pack.getName();
			if(len(pack.getImplementationVersion())) {
				packinfo["version"] = pack.getImplementationVersion();
			}
			if(len(pack.getSpecificationVersion())) {
				packinfo["specver"] = pack.getSpecificationVersion();
			}
			if(len(pack.getSpecificationTitle())) {
				packinfo["title"] = pack.getSpecificationTitle();
			}
				
			allpacks[x] = packinfo;
		}
		dumpvar(allpacks);
	</cfscript>
</cffunction>


<cffunction name="getConfigPath" access="private" returntype="string" output="false">
	<cfreturn variables.ConfigPath />
</cffunction>

<cffunction name="setConfigPath" access="private" returntype="void" output="false">
	<cfargument name="configXmlFile" type="string" required="true">
	<cfargument name="hbmXmlDir" type="string" required="true">
	<cfargument name="javafilesdir" type="string" required="true">
	<cfargument name="classfilesdir" type="string" required="true">
	<cfargument name="reverseEngDir" type="string" required="true">
	<cfargument name="hbmExportDir" type="string" required="true">
	<cfset var arg = ""/>
	<cfloop list="#structKeyList(arguments)#" index="arg">
		<cfset variables[arg] = expandPath(arguments[arg]) />
	</cfloop>
</cffunction>

</cfcomponent>